# @Time : 2021/3/19 16:07
# @Author : Li Kunlun
# @Description : 求解B所对应的支配解集（A，F）
import numpy as np
import pandas as pd

d = {
    'A': [20, 2.2],
    'B': [60, 4.4],
    'C': [65, 3.5],
    'D': [15, 4.4],
    'E': [55, 4.5],
    'F': [50, 1.8],
    'G': [80, 4.0],
    'H': [25, 4.6]
}
# A     B     C     D     E     F     G     H
# 0  20.0  60.0  65.0  15.0  55.0  50.0  80.0  25.0
# 1   2.2   4.4   3.5   4.4   4.5   1.8   4.0   4.6
# df1 = pd.DataFrame(data=d)
# print("未转置--", df1)
# 转置之后输出
#       0    1
# A  20.0  2.2
# B  60.0  4.4
# C  65.0  3.5
# D  15.0  4.4
# E  55.0  4.5
# F  50.0  1.8
# G  80.0  4.0
# H  25.0  4.6
df = pd.DataFrame(data=d).T
# print("---df--", df)
data_labels = list(df.index)
# ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H']
# print("data_labels---", data_labels)
data_array = np.array(df).T
#  [[20.  60.  65.  15.  55.  50.  80.  25. ]
#  [ 2.2  4.4  3.5  4.4  4.5  1.8  4.   4.6]]
# print("data_array---", data_array)

# 指定解决的索引
sol_index = 1
sol = data_array[:, sol_index]
#  [60.   4.4]
# print("sol---", sol)
# (array([0, 1, 3, 4, 5, 7], dtype=int64),)
# print("------无[0]输出--------", np.where(sol[0] >= data_array[0, :]))
obj1_not_worse = np.where(sol[0] >= data_array[0, :])[0]
# np.where表示返回下标
# obj1_not_worse------ [0 1 3 4 5 7]
# obj2_not_worse------ [0 1 2 3 5 6]
# print("obj1_not_worse------", obj1_not_worse)
obj2_not_worse = np.where(sol[1] >= data_array[1, :])[0]
# print("obj2_not_worse------", obj2_not_worse)
not_worse_candidates = set.intersection(set(obj1_not_worse), set(obj2_not_worse))
# 用于返回两个或更多集合中都包含的元素，即交集。
# not_worse_candidates-- {0, 1, 3, 5}
# print("not_worse_candidates--", not_worse_candidates)

obj1_better = np.where(sol[0] > data_array[0, :])[0]
obj2_better = np.where(sol[1] > data_array[1, :])[0]
better_candidates = set.intersection(set(obj1_better), set(obj2_better))
# 从候选解中进行筛选，返回优解。
# better_candidates-- {0, 5}
# print("better_candidates--", better_candidates)

dominating_solution = list(set.intersection(not_worse_candidates, better_candidates))
# 候选解和优解交叉，选出支配解。
# dominating_solution-- [0, 5]
# print("dominating_solution--", dominating_solution)

if len(dominating_solution) == 0:
    print("No solution dominates solution", data_labels[sol_index], ".")
else:
    # 输出：Labels of one or more solutions dominating this solution :A,F,
    print("Labels of one or more solutions dominating this solution :", end="")
    for k in dominating_solution:
        print(data_labels[k], end=',')
